package com.svpino.netflix.model;

import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.svpino.netflix.ProxyImpl;
import com.svpino.netflix.LateBindingList;
import com.svpino.netflix.parsers.FieldParser;
import com.svpino.netflix.parsers.FormatParser;


public class Title extends Item {

	private final ProxyImpl connection;
	private final String id;
	private final String title;
	private final String shortTitle;
	private final int releaseYear;
	private final float rating;
	private final Integer runtime;
	private final String mpaaRating;
	private String synopsis;
	private Element synopsisElement;
	private final Element catalogTitleElement;
	private LateBindingList<Format> formats;

	public Title(
			ProxyImpl connection,
			String id,
			String title, 
			String shortTitle, 
			int releaseYear, 
			float rating,
			Integer runtime,
			String mpaaRating,
			Element catalogTitleElement) {
		this.connection = connection;
		this.id = id;
		this.title = title;
		this.shortTitle = title;
		this.releaseYear = releaseYear;
		this.rating = rating;
		this.runtime = runtime;
		this.mpaaRating = mpaaRating;
		this.synopsis = null;
		this.catalogTitleElement = catalogTitleElement;
		
		initialize();
	}
	
	private void initialize() {
		try {
			XPathFactory factory = XPathFactory.newInstance();
			XPath xPath = factory.newXPath();
			Element formatsElement = (Element)((NodeList)xPath.compile("./link[@rel='http://schemas.netflix.com/catalog/titles/format_availability']").evaluate(this.catalogTitleElement, XPathConstants.NODESET)).item(0);

			if (formatsElement == null || formatsElement.getChildNodes().getLength() == 0) {
				this.formats = new LateBindingList<Format>(new FormatParser(this.connection), 
					this.id + "/format_availability",
					this.id + "/screen_formats");
			}
			else {
				Element screenFormatsElement = (Element)((NodeList)xPath.compile("./link[@rel='http://schemas.netflix.com/catalog/titles/screen_formats']").evaluate(this.catalogTitleElement, XPathConstants.NODESET)).item(0);
				Element languagesAndAudioElement = (Element)((NodeList)xPath.compile("./link[@rel='http://schemas.netflix.com/catalog/titles/languages_and_audio']").evaluate(this.catalogTitleElement, XPathConstants.NODESET)).item(0);
				
				this.formats = new LateBindingList<Format>(new FormatParser(this.connection), 
					formatsElement, 
					screenFormatsElement,
					languagesAndAudioElement);
			}
			
			this.synopsisElement = (Element)((NodeList)xPath.compile("./link[@rel='http://schemas.netflix.com/catalog/titles/synopsis']").evaluate(this.catalogTitleElement, XPathConstants.NODESET)).item(0);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}

	public String getTitle() {
		return this.title;
	}

	public String getShortTitle() {
		return this.shortTitle;
	}

	public Integer getReleaseYear() {
		return this.releaseYear;
	}

	public float getRating() {
		return this.rating;
	}
	
	public Integer getRuntime() {
		return this.runtime;
	}
	
	public String getMpaaRating() {
		return this.mpaaRating;
	}
	
	public String getSynopsis() throws Exception {
		return null;
	}
	
	public LateBindingList<Format> getFormats() {
		return this.formats;
	}
}
